"""
author：wang tian cheng
date: 2020/11/1
project: ldj
"""
###三乐购物系统 都取整数




while True:
    print("登录请按1，注册用户请按2，退出请按quit")
    print("请输入选项: ",end="")
    use=input()
    if use=="quit":
        break
    if use=="2":
        username = input("用户名注册: ")
        passwd = input("密码注册: ")
        with open("user.txt","a") as fp1:
            with open("user.txt", "r") as  fp_use:
                list_user1 = fp_use.readline()
                s = list_user1.split()
                lst1= []
                print(s)
                for item1 in s:
                    lst1.append(item1.split(":"))
                print(type(dict(lst1)),dict(lst1))
                if dict(lst1)=={}:
                    print("注册成功，首次注册，免费送1000元")
                    user = f"{username}:{passwd} "
                    fp1.write(f'{user}')
                    with open(f"{username}money.txt", "a", encoding="UTF-8") as fp:
                        a="1000"
                        fp.write(a)
                    continue
                else:
                    for j, k in dict(lst1).items():
                        if j != username:
                            print("注册成功，首次注册，免费送1000元")
                            user = f"{username}:{passwd} "
                            fp1.write(f'{user}')
                            with open(f"{username}money.txt", "a", encoding="UTF-8") as fp:
                                a = "1000"
                                fp.write(a)

                            break
                        else:
                            print("用户名已存在")
                            break
        continue
    if use=="1":
        username = input("用户名: ")
        passwd = input("密码: ")

        with open("user.txt","a") as fp2:
            a=''
            fp2.write(a)
        with open("user.txt","r") as fp2:
            list_user=fp2.readline()
            s=list_user.split()
            lst2=[]
            print(s)
            for item in s:
                lst2.append(item.split(":"))
            if s==[]:
                print("用户名或密码错误")
                continue
            for j,k in dict(lst2).items():
                if j==username and k==passwd:
                    print("登录成功")
                    with open(f"{username}money.txt", "a", encoding="UTF-8") as fp:
                        a=""
                        fp.write(a)
                    while True:
                        print("欢迎进入三乐购物系统")
                        print("请选择你的预算: ")
                        money_user=input()
                        gift=["苹果","梨子","香蕉","桃子","车厘子","葡萄"]
                        print("[1.苹果:8] [ 2.梨子:18 ] [3香蕉:28] [4.桃子:68],[5.车厘子:188] [6葡萄:98]")
                        money_sum=int()
                        while True:
                            print("选择购买按1，查看余额按2，退出按系统按3,清空购物车按4，查看购物车按5,结算请按6,充值请按7")
                            aaa=input()
                            if aaa=="3":
                                break
                            if aaa=="2":
                                with open(f"{username}money.txt","r",encoding="UTF-8") as fp_money:
                                    print(fp_money.readline())
                                continue
                            if aaa=="4":
                                with open(f"{username}history1.txt","w",encoding="UTF-8") as fp_history:
                                    history=""
                                    fp_history.write(history)
                                money_sum=0
                            if aaa=="5":
                                with open(f"{username}history1.txt","r",encoding="UTF-8") as fp_history1:
                                    list_user1 = fp_history1.readline()
                                    s1= list_user1.split()
                                    lst21 = []
                                    lst211=[]
                                    for item in s1:
                                        lst21.append(item.split(":"))
                                        lst211=lst21
                                    print(lst211)
                            if aaa == "7":
                                print("输入充值金额: ")
                                money_money = input()
                                with open(f"{username}money.txt", "r", encoding="UTF-8") as fp:
                                    use_1 = fp.readline()
                                    use_2 = str(int(use_1) + int(money_money))
                                    with open(f"{username}money.txt", "w", encoding="UTF-8") as fp1:
                                        fp1.write(use_2)
                            if aaa=="1":
                                while True:
                                    print("选择您需要的商品号,退出请按quit: ")
                                    num1=input()
                                    if num1=="quit":
                                        break
                                    print("选择你需要的数量: ")
                                    sum1=input()
                                    if num1=="1":
                                        money6=str(int(sum1)*8)
                                        print(money6)
                                        money_sum+=(int(sum1)*8)
                                        with open(f"{username}history1.txt", "a",encoding="UTF-8") as fp_history1:
                                            history1 = f"{gift[0]}:{money6} "
                                            fp_history1.write(history1)
                                    if num1=="2":
                                        money6 = str(int(sum1) * 18)
                                        money_sum += (int(sum1) * 18)

                                        with open(f"{username}history1.txt", "a",encoding="UTF-8") as fp_history1:
                                            history1 = f"{gift[1]}:{money6} "
                                            fp_history1.write(history1)
                                    if num1 =="3":
                                        money6 = str(int(sum1) * 28)
                                        money_sum += (int(sum1) * 28)

                                        with open(f"{username}history1.txt", "a",encoding="UTF-8") as fp_history1:
                                            history1 = f"{gift[2]}:{money6} "
                                            fp_history1.write(history1)
                                    if num1 =="4":
                                        money6 = str(int(sum1) * 68)
                                        money_sum += (int(sum1) * 68)

                                        with open(f"{username}history1.txt", "a",encoding="UTF-8") as fp_history1:
                                            history1 = f"{gift[3]}:{money6} "
                                            fp_history1.write(history1)
                                    if num1 == "5":
                                        money6 = str(int(sum1) * 188)
                                        money_sum += (int(sum1) * 188)

                                        with open(f"{username}history1.txt", "a",encoding="UTF-8") as fp_history1:
                                            history1 = f"{gift[4]}:{money6} "
                                            fp_history1.write(history1)
                                    if num1 == "6":
                                        money6 = str(int(sum1) * 98)
                                        money_sum += (int(sum1) * 98)

                                        with open(f"{username}history1.txt","a",encoding="UTF-8") as fp_history1:
                                            history1=f"{gift[5]}:{money6} "
                                            fp_history1.write(history1)

                                    print("您已消费", money_sum, "元")
                                    continue
                                continue
                            if aaa=="6":

                                print("您已消费", money_sum, "元")
                                if int(money_sum)>int(money_user):
                                    print("你的消费超出您的预期，请重新选择物品")
                                    with open(f"{username}history1.txt", "w") as fp_history1:
                                        history1 = ""
                                        fp_history1.write(history1)
                                    continue
                                else:
                                    with open(f"{username}money.txt", "r", encoding="UTF-8") as fp:
                                        use_1 = fp.readline()
                                        use_2 =str(int(use_1)-int(money_sum))
                                        with open(f"{username}money.txt", "w", encoding="UTF-8") as fp1:
                                            fp1.write(use_2)
                                    with open(f"{username}history1.txt","w", encoding="UTF-8") as fp_history1:
                                        history1 = ""
                                        fp_history1.write(history1)
                                break
                            continue
                        break
                    break
                if j!=username or k!=passwd:
                    print("用户名或密码错误")
                    break
